#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "../DEMURA/Demura.h"
#include "usbd_cdc.h"

/********************************************************************************************
nt37700c 639 Dante
********************************************************************************************
********************************************************************************************/

void NT37700_Enable_Flash_Control_and_SPI_Flash_En(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];	

	NT37700C_QE1(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	
	delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}
void NT37700_Inter_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];

	NT37700C_Program_Flash_Set(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);

	buffer[0] = 0x2F;
	buffer[1] = 0x01;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}
                                    
void NT37700_Exit_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                               
void NT37700_Flash_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u32 error_times=0;
		u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
		
		NT37700C_Program_Flash_Load(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       

void NT37700_Flash_Write_Demura2(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}


void NT37700_Flash_Check_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[3];
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x04;					
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  	
}
                                  
void NT37700_Flash_Erase_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	
	NT37700C_Erase_Flash(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x00;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú	
}

void NT37700_Flash_Demura_OTP(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}

void NT37700_Demura_Function_ON(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	
	buffer1[0] = 0x06;
	buffer1[1] = 0xF0;
	buffer1[2] = 0x55;
	buffer1[3] = 0xAA;
	buffer1[4] = 0x52;
	buffer1[5] = 0x08;
	buffer1[6] = 0x04;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);	
	buffer1[0] = 0x06;
	buffer1[1] = 0xD0;
	buffer1[2] = 0x00;
	buffer1[3] = 0x7F;
	buffer1[4] = 0x00;
	buffer1[5] = 0x00;
	buffer1[6] = 0x84; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x07;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú
}

void NT37700_Demura_Function_OFF(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	buffer1[0] = 0x06;
	buffer1[1] = 0xF0;
	buffer1[2] = 0x55;
	buffer1[3] = 0xAA;
	buffer1[4] = 0x52;
	buffer1[5] = 0x08;
	buffer1[6] = 0x04;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);		
	buffer1[0] = 0x06;
	buffer1[1] = 0xD0;
	buffer1[2] = 0x00;
	buffer1[3] = 0x7F;
	buffer1[4] = 0x00;
	buffer1[5] = 0x00;
	buffer1[6] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	delay_ms(5);
	buffer[0] = 0x2F;  //Demura_type
	buffer[1] = 0x08;				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}


void NT37700C_GM820_Write_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	static u8 test0=0;
	//u8 buf2PC[10];
	/*DemuraIctype->IC型号; Demura_OLEDModule_flag->项目号*/
	USB_Rx_Demura_End_Flag=0;	
	if(USB_Rx_Demura==0)//第一笔数据
	{												
		test0=0;
		//else if(DemuraIctype==0x5D)//novateck
		DemuraTransOnce=180*3;
		USB_Rx_Demura=1; //进入Demura数据写入状态														
	}
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //写入单笔数据,单次写入DemuraTransOnce个字节
	{												
		//else if(DemuraIctype==0x5D) //novateck
		if(Demura_first_package_flag==0)
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBC,180*3+1);  
			SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
			//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xBE,(u16) (180*3));
			SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
			SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x4c));  //模拟4C 
			Demura_first_package_flag=1;
		}
		else
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBC,180*3+1);  	
			SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
			SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x5c)); 
		}											
		SPI_CS_Select(channel,0);
		SSD2828_W_RAM_demura_buffer_part_bgr(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce,1,0);
		SPI_CS_Select(channel,1);										
				
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			Demura_START_FLAG=0;  //清除Demura状态
			DemuraRecivedNum=0; //累计接收统计归零
			USB_Rx_Demura=0;//进入Demura downloading标志
			Demura_first_package_flag=0; //Novatek_NT37700C 4c/5c
			//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xC0,0x0001);
		//	STM2PC(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
		}
	}
}


void ProcessForDmuR07( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
					NT37700_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					NT37700_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					NT37700_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					NT37700_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					NT37700_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					NT37700_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					NT37700_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					NT37700_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					NT37700_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************